﻿#include "BrowserTableHandlerMaterialVisESolid.h"
#include "OperatorsInterface/OperAbstractMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsViscoElastic.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

namespace GUI
{
    void GUI::BrowserTableHandlerMaterialVisESolid::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Density [kg/m3]") << tr("Poisson's Ratio") <<
            tr("Shear Modulus Spectrum") << tr("DLF Spectrum") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void GUI::BrowserTableHandlerMaterialVisESolid::updateData()
    {
        if (_tableWidget == nullptr) return;
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        //获取材料管理器
        Acoustics::FITKAcousticsMaterialManager* matmgr = phys->getMaterialManager();
        if (matmgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsMaterial*> matList = matmgr->getAcousticsMaterials(
            Acoustics::FITKAcousticsAbsMaterial::ViscoElastic);
        //材料数量
        const int n = matList.size();
        _tableWidget->setRowCount(n);

        Acoustics::FITKAcousticsPhysics* physics = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (!physics)return;
        auto spectraManager = physics->getFITKAcousticsSpectraManager();
        if (!spectraManager)return;

        for (int i = 0; i < n; ++i)
        {
            //转换为粘弹性固体材料
            Acoustics::FITKAcousticsMaterialsViscoElastic* maV = dynamic_cast<Acoustics::FITKAcousticsMaterialsViscoElastic*>
                (matList.at(i));
            if (maV == nullptr) continue;
            //数据ID
            const int id = maV->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(maV->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(maV->getDensity()), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(maV->getPoissonsRatio()), id));

            auto shear = spectraManager->getDataByID(maV->getShearModulusSpectrumID());
            auto damp = spectraManager->getDataByID(maV->getDampingSpectrumID());
            if (shear)
                _tableWidget->setItem(i, 3, new QTableWidgetItem(shear->getDataObjectName(), id));
            if (damp)
                _tableWidget->setItem(i, 4, new QTableWidgetItem(damp->getDataObjectName(), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(maV->getMaterialDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractMaterial * GUI::BrowserTableHandlerMaterialVisESolid::getMaterialOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractMaterial>("OperMaterialViscoElastic");
    }

    bool GUI::BrowserTableHandlerMaterialVisESolid::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //只有密度能批量
        if (colIndex != 1) return false;

        return true;
    }
}

